<?php
namespace Api\Service;

/**
 * 购物车
 */
class CartService
{
    /**
     * [$cart description]
     * @var null
     */
    public $shop_goods = null;
    /**
     * [$goods description]
     * @var null
     */
    public $goods = null;
    public $shop  = null;

    /**
     * 购物车列表
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function lists($user_id)
    {
        $this->goods = $goodss = M('cart')
            ->field('id,shop_id,goods_id,spec_item_id,goods_name,goods_img,price,number,(price * number) total')
            ->where([
                'user_id'   => $user_id,
                'is_delete' => 0,
            ])
            ->order('id desc')
            ->select();

        $shop_ids   = [];
        $shop_goods = [];

        // 购物车里有商品
        if (count($goodss)) {
            foreach ($goodss as $key => &$goods) {
                $shop_ids[] = $goods['shop_id'];

                $goods['spec_item_str'] = D('Api/Goods', 'Service')->getSpecItemStr($goods['spec_item_id']);

                $shop_goods[$goods['shop_id']][] = $goods;
            }

            $shops = M('shop')
                ->field('id,name,logo')
                ->where(['id' => ['in', $shop_ids]])
                ->lists('id');

            foreach ($shops as $id => $shop) {
                $shops[$id]['goods'] = $shop_goods[$id];
            }
            $shop_goods = $shops;
        }

        return [
            'shop_goods' => array_values($shop_goods),
            'number'     => $this->number(),
            'total'      => $this->total(),
        ];
    }

    /**
     * 加入购物车
     * @param [type] $user_id  [description]
     * @param [type] $goods_id [description]
     * @param [type] $sp_it_id [description]
     * @param [type] $number   [description]
     */
    public function add($user_id, $goods_id, $sp_it_id, $number)
    {
        $cart = M('cart')
            ->field('id')
            ->where([
                'user_id'      => $user_id,
                'goods_id'     => $goods_id,
                'spec_item_id' => $sp_it_id,
                'is_delete'    => 0,
            ])->find();
        if ($cart) {
            return $this->inc($user_id, $cart['id'], $number);
        }

        $goods = M('goods')
            ->field('shop_id,title,image,description,price,express_fee')
            ->find($goods_id);

        $data = [
            'shop_id'      => $goods['shop_id'],
            'user_id'      => $user_id,
            'goods_id'     => $goods_id,
            'spec_item_id' => $sp_it_id,
            'goods_name'   => $goods['title'],
            'goods_img'    => $goods['image'],
            'price'        => $goods['price'],
            'number'       => $number,
            'add_time'     => CUR_DATETIME,
        ];

        // 是否有规格属性
        if ($sp_it_id) {
            $spec_item = M('goods_spec_item')
                ->field('name,price,stock,express_fee,is_sales')
                ->find($sp_it_id);

            $data['goods_name'] .= ' ' . $spec_item['name'];
            $data['price'] = $spec_item['price'];
        }

        $res = M('cart')->add($data);

        return $res;
    }

    /**
     * 删除商品
     * @param  [type] $user_id [description]
     * @param  [type] $add_id  [description]
     * @return [type]          [description]
     */
    public function delete($user_id, $cart_id)
    {
        $data = [
            'is_delete'   => 1,
            'delete_time' => CUR_DATETIME,
        ];
        $res = M('cart')->where([
            'id'      => ['in', explode(',', $cart_id)],
            'user_id' => $user_id,
        ])->save($data);

        return $res;
    }

    /**
     * 清空购物车
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function clean($user_id)
    {
        $data = [
            'is_delete'   => 1,
            'delete_time' => CUR_DATETIME,
        ];
        $res = M('cart')->where([
            'is_delete' => 0,
            'user_id'   => $user_id,
        ])->save($data);

        return $res;
    }

    /**
     * [数量增加]
     * @param  [type] $user_id [description]
     * @param  [type] $cart_id [description]
     * @param  [type] $number  [description]
     * @return [type]          [description]
     */
    public function inc($user_id, $cart_id, $number)
    {
        $res = M('cart')->where([
            'id'        => $cart_id,
            'user_id'   => $user_id,
            'is_delete' => 0,
        ])->setInc('number', $number);
        return $res;
    }

    /**
     * [数量-1]
     * @param  [type] $user_id [description]
     * @param  [type] $cart_id [description]
     * @param  [type] $number  [description]
     * @return [type]          [description]
     */
    public function dec($user_id, $cart_id, $number)
    {
        $cart_number = M('cart')
            ->where(['id' => $cart_id])
            ->getField('number');

        if ($cart_number <= $number) {
            return $this->delete($user_id, $cart_id);
        }

        $res = M('cart')->where([
            'id'        => $cart_id,
            'user_id'   => $user_id,
            'is_delete' => 0,
        ])->setDec('number', $number);

        return $res;
    }

    /**
     * 计算数量
     * @return [type] [description]
     */
    protected function number()
    {
        $goodss = $this->goods;
        $number = 0;
        if (count($goodss)) {
            foreach ($goodss as $goods) {
                $number += $goods['number'];
            }
        }

        return $number;
    }

    /**
     * 计算总价
     * @return [type] [description]
     */
    protected function total()
    {
        $goodss = $this->goods;
        $price  = 0;
        if (count($goodss)) {
            foreach ($goodss as $goods) {
                $price += $goods['total'];
            }
        }

        return round($price, 2);
    }
}
